[Coding035] LeetCode 86

Partition List

Ben 2024.03.27

More coding records

Get the knowledge flowing and circulating! :)

目录

本题收获

题目:86. Partition List

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example 1:

img

Example 2:

Constraints:


代码1(配 · 手绘过程图)

image-20240327180238996

代码解读 | 评价

解题思路

image-20240327180319486

  • 代码有点复杂,不够清晰;

  • p、q、r这3个指针虽然有逻辑,但是没法让自己一看就能看得十分清楚!

复杂度分析

image-20240327180059048


代码2(配 · 手绘过程图)

image-20240327180828680

代码解读 | 评价

  • 代码比较清晰。十分舒爽!

复杂度分析

image-20240327180841695